package com.gw2.gw2world;

import com.gw2.gw2world.controller.NavigationManager;
import com.gw2.gw2world.controller.Tools;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;

/** Load data before the application start. Loaded data are:
 * <ul>
 * <li>Database: create it if not installed</li>
 * </ul>
 */
public class HomeActivity extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = "HomeActivity";
	private Button b_world_events, b_active_events, b_td, b_config;
	private Intent intent;
	private NavigationManager nm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Tools.forceLocale(this);
		setContentView(R.layout.home_screen);
		b_world_events = (Button) findViewById(R.id.b_world_event);
		b_world_events.setOnClickListener(this);
		b_active_events = (Button) findViewById(R.id.b_active_event);
		b_active_events.setOnClickListener(this);
		b_td=(Button) findViewById(R.id.b_temple_dungeon);
		b_td.setOnClickListener(this);
		b_config = (Button) findViewById(R.id.b_settings);
		b_config.setOnClickListener(this);
		nm=new NavigationManager(this, R.id.drawer_layout_home, R.id.left_drawer_home);
	}
	@Override
	protected void onResume() {
		Tools.lockScreenFromPreferences(this);
		super.onResume();
	}
	@Override
	protected void onPause() {
		Tools.unlockScreen(this);
		super.onPause();
	}
	
	/**
	 * Run the next activity depending on what button was clicked
	 * {@link WorldEventActivity} or {@link ActiveEventActivity} or {@link SettingsActivity}
	 */
	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.b_world_event)
			intent=new Intent(HomeActivity.this, WorldEventActivity.class);
		else if(v.getId()==R.id.b_active_event)
			intent=new Intent(HomeActivity.this, ActiveEventActivity.class);
		else if(v.getId()==R.id.b_settings)
			intent=new Intent(HomeActivity.this, SettingsActivity.class);
		else if(v.getId()==R.id.b_temple_dungeon)
			intent=new Intent(HomeActivity.this, TempleDungeonMenuActivity.class);
		else return;
		startActivity(intent);
	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //MenuInflater inflater = getMenuInflater();
        //inflater.inflate(R.menu.actionbar_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }
	@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        //boolean drawerOpen = nm.isDrawerOpen();
        //menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (nm.getDrawerToggle().onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...
        return super.onOptionsItemSelected(item);
	}
	@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        nm.getDrawerToggle().syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        nm.getDrawerToggle().onConfigurationChanged(newConfig);
    }
}
